package com.yuyou.fn.settlement.mapper;

import com.github.wens.mybatisplus.mapper.AutoMapper;
import com.yuyou.fn.settlement.entity.MainAccount;
import com.yuyou.fn.settlement.vo.AccountReportQueryVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.math.BigDecimal;
import java.util.Date;

/**
 *
 * MainAccount 表数据库控制层接口
 *
 */
public interface MainAccountMapper extends AutoMapper<MainAccount,Long> {


    @Select("<script>\n" +
            "select sum(amount) from change_record \n" +
            "<where>\n" +
            "<if test=\"query.mainAccountId != null \">\n" +
            "\tand main_account_id = #{query.mainAccountId}\n" +
            "</if>\n" +
            "<if test=\"query.subject != null \">\n" +
            "\tand subject = #{query.subject}\n" +
            "</if>\n" +
            "\n" +
            "<if test=\"query.startDate != null \">\n" +
            "<![CDATA[\n" +
            "\tand create_time > #{query.startDate}\n" +
            "]]>\n" +
            "</if>\n" +
            "\n" +
            "<if test=\"query.endDate != null \">\n" +
            "<![CDATA[\n" +
            "\tand create_time < #{query.endDate}\n" +
            "]]>\n" +
            "</if>\n" +
            "\n" +
            "<if test=\"onlyPositive != null \">\n" +
            "<![CDATA[\n" +
            "\tand amount > 0 \n" +
            "]]>\n" +
            "</if>\n" +
            "</where>\n" +
            "</script>")
    BigDecimal sumAmountByQuery(@Param("query") AccountReportQueryVo accountReportQueryVo, @Param("onlyPositive") boolean onlyPositive);

    @Select("<script>\n" +
            "<![CDATA[\n" +
            "select sum(amount) from change_record where main_account_id = #{mainAccountId} and subject = #{subject} and create_time > #{startDate}  and create_time < #{endDate} and settle_status = 0 and amount > 0 \n" +
            "]]>\n" +
            "</script>")
    BigDecimal sumNotSettleAmount(@Param("mainAccountId") Long mainAccountId, @Param("subject") String subject, @Param("startDate") Date startDate, @Param("endDate") Date endDate);

    @Update("update main_account set balance_amount = balance_amount + #{balance} where main_account_id = #{mainAccountId}" )
    void increaseBalance(@Param("mainAccountId") Long mainAccountId, @Param("balance") BigDecimal balance);

    @Update("update main_account set lock_balance_amount = lock_balance_amount + #{amount} , balance_amount = balance_amount - #{amount}  where main_account_id = #{mainAccountId} and #{amount} <= balance_amount"  )
    int lockBalanceAmount(@Param("mainAccountId") Long mainAccountId, @Param("amount") BigDecimal amount);

    @Update("update main_account set lock_balance_amount = lock_balance_amount - #{amount} , balance_amount = balance_amount + #{amount}  where main_account_id = #{mainAccountId} and #{amount} <= lock_balance_amount"  )
    int unlockBalanceAmount(@Param("mainAccountId") Long mainAccountId, @Param("amount") BigDecimal amount);

    @Update("UPDATE main_account target, main_account origin  set target.balance_amount=target.balance_amount+origin.balance_amount,target.lock_balance_amount=target.lock_balance_amount+origin.lock_balance_amount where target.main_account_id=#{targetMainAccountId} and origin.main_account_id= #{originMainAccountId}")
    int mergeBalanceByOriginAndTargetId(@Param("originMainAccountId") long originMainAccountId,@Param("targetMainAccountId") long targetMainAccountId);
}